package com.llamarun;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;

public class Llama extends GameObject{
	private ArrayList<Projectible> projectibles;
	private PowerUp powerUp = null;
	private boolean powerUpInUse = false;
	private int movementConstant = 5;
	private Bitmap smallLlama = null;
	private Bitmap bigLlama = null;
	private View view = null;
	
	public Llama (View view)
	{
		this.view = view;
		this.projectibles = new ArrayList<Projectible>();
		this.setBitmap(BitmapFactory.decodeResource(view.getResources(), R.drawable.llama2));
		smallLlama = BitmapFactory.decodeResource(view.getResources(), R.drawable.llama3);
		bigLlama = BitmapFactory.decodeResource(view.getResources(), R.drawable.llama2);
		this.setX(150);
		this.setY(200);
	}
	
	public void draw(Canvas canvas) {
		canvas.drawBitmap(this.getBitmap(), this.getX() - (this.getBitmap().getWidth() / 2), this.getY() - (this.getBitmap().getHeight() / 2), null);
		int totalX = this.getBitmap().getWidth()/2;
		int size = projectibles.size();
		for (int i=0; i<size; i++)
		{
			//attempting to fix bug where sometimes size = index of what you are trying to shoot
			if (i<projectibles.size()) {
			  projectibles.get(i).setX(this.getX() - totalX);
			  projectibles.get(i).setY(this.getY());
			  canvas.drawBitmap(projectibles.get(i).getBitmap(), projectibles.get(i).getX(), projectibles.get(i).getY()  /**-(p.getBitmap().getHeight()/2)**/, null);
			  totalX+=projectibles.get(i).getBitmap().getWidth();
			}
		}
		
		//calls method to check for a powerup and uses it
		usePowerUp();
	
		
	}
	
	private void usePowerUp() {
		if(powerUp!=null) {
			this.powerUp.start(this);
		}
		/*if(powerUp!=null)
		{
			if(powerUpInUse == false)
			{
				this.powerUp.start(this);
				this.powerUpInUse = true;
			}
			
			if(powerUp.getX()<0)
			{
				if(powerUpInUse)
				{
					this.powerUp.end(this);
					this.powerUpInUse = false;
					this.powerUp = null;
				}
			}
		}*/
	}

	public Bitmap getSmallLlama()
	{
		return smallLlama;
	}
	
	public Bitmap getBigLlama()
	{
		return bigLlama;
	}
	
	public ArrayList<Projectible>getProjectibles()
	{
		return projectibles;
	}
	
	public void shootProjectible()
	{
		if(!projectibles.isEmpty()){
		projectibles.get(0).shoot();
		projectibles.remove(0);
		}
	}
	
	public void jump()
	{
		
	}
	
	public void setMovementSpeed(int speed)
	{
		this.movementConstant = speed;
	}
	
	public void setPowerUp(PowerUp powerUp)
	{
		/*if(this.powerUp!= null){
			this.powerUp.end(this);
			this.powerUpInUse = false;
			this.powerUp = null;
		}
		this.powerUp = powerUp;
		//sets it off the screen and at the x coordinate of the scree
		//which gives us a psuedo timer...
		powerUp.setY(-view.getHeight());
		powerUp.setX(view.getWidth());*/
		
		if(this.powerUp!=null){
			this.powerUp.end(this);
			this.powerUp.inUse = false;
			this.powerUp.setX(-20);
			this.powerUp = null;
		}
		this.powerUp = powerUp;
	}
	
	public void moveUp(int time)
	{
		if(this.getY() >= 32)
		{
			this.setY(this.getY()-movementConstant*time/5);

		}
	}
	
	public void moveDown(int time)
	{
		if(this.getY() <= (view.getHeight() - 32))
		{
			this.setY(this.getY()+movementConstant*time/3);
		}
	}

	public void addProjectible(Projectible p) {
		this.projectibles.add(p);
	}

	public void improveProjectibles() {
		for (Projectible p : this.projectibles)
		{
			p.improve();
		}
	}
	
	public void unimproveProjectibles() {
	  for (Projectible p : this.projectibles) {
		  p.unimprove();
	  }
	}
	
	public void die()
	{
		((GameView) view).gameOver();
	}
	
}
